package com.randi.file.entity;

import com.baomidou.mybatisplus.annotation.*;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.randi.common.core.domain.BaseParam;
import com.randi.file.enums.MineEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.data.annotation.Transient;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.math.BigInteger;
import java.util.Date;

/**
 * @author ziye
 */
@Data
@TableName("sys_file")
@ApiModel(value = "SysFile对象", description = "文件信息")
public class SysFile {

    @TableField(exist = false)
    @JsonSerialize(using = ToStringSerializer.class)
    @ApiModelProperty(value = "key")
    private Long key;

    @TableId(value = "file_id", type = IdType.ASSIGN_ID)
    @JsonSerialize(using = ToStringSerializer.class)
    @ApiModelProperty(value = "ID")
    private Long fileId;

    @NotBlank(message = "文件名不能为空")
    @TableField(value = "`file_name`")
    @ApiModelProperty(value = "文件名")
    private String fileName;

    @TableField(value = "`mine_type`")
    @ApiModelProperty(value = "文件类型")
    private MineEnum mineType = MineEnum.$OTHER;

    @NotBlank(message = "文件后缀不能为空")
    @TableField(value = "`file_suffix`")
    @ApiModelProperty("文件后缀")
    private String fileSuffix;

    @NotBlank(message = "哈希值不能为空")
    @TableField(value = "`file_hash`")
    @ApiModelProperty(value = "哈希值")
    private String fileHash;

    @NotNull(message = "文件大小不能为空")
    @TableField(value = "`file_size`")
    @ApiModelProperty(value = "文件大小")
    private BigInteger fileSize;

    @TableField(value = "`deleted`")
    @ApiModelProperty(value = "是否删除")
    private Integer deleted;

    @TableField(value = "`create_time`")
    @ApiModelProperty(value = "上传时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date createTime;

    @JsonInclude(JsonInclude.Include.NON_EMPTY)
    @ApiModelProperty(value = "检索参数")
    private BaseParam params;

    @Transient
    public String getKey() {
        return this.fileId == null ? "" : this.fileId.toString();
    }

    public void setMineType(MineEnum mineEnum) {
        this.mineType = mineEnum;
    }

    public void setMineType(String value) {
        this.mineType = MineEnum.valueOfAuto(value);
    }

    public String getMineValue() {
        return mineType.getValue();
    }

    public String getFileType() {
        return mineType.getFileType().getValue();
    }

    public String getFileTypeLabel() {
        return mineType.getFileType().getLabel();
    }

    public Boolean getIsDeleted() {
        return deleted != null && deleted != 0;
    }

    public String getFileNamePro() {
        return fileName + "." + fileSuffix;
    }
}
